using System;
using System.Collections;
using System.Collections.Generic;
using TinyJson;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Sauce.Internal.GDPR.Utils;

namespace Voodoo.Sauce.Internal.GDPR.UI
{
	public class VoodooGDPRCanvas : MonoBehaviour
	{
		private static class GDPRScreen
		{
			public const int NoScreen = -1;

			public const int PrivacyScreen = 0;

			public const int DataSummaryScreen = 1;

			public const int DataSharingSettingsScreen = 2;

			public const int WarningScreen = 3;

			public const int LastWarningScreen = 4;

			public const int WarningBanner = 5;

			public const int SettingsScreen = 6;

			public const int AccessModifyScreen = 7;

			public const int DeleteScreen = 8;

			public const int MessageDialogScreen = 9;
		}

		private const string TAG = "VoodooGDPRCanvas";

		private static VoodooGDPRCanvas _instance;

		[SerializeField]
		private GameObject[] _screens;

		[SerializeField]
		private RectTransform[] _privacyPoliciesContent;

		[SerializeField]
		private VoodooGDPRMessageDialog _messageDialog;

		[SerializeField]
		private GameObject _privacyPolicyPrefab;

		private Dictionary<string, object> _jsonData;

		private int _currentScreen;

		private Color _mainColor;

		private bool _adsConsent;

		private bool _analyticsConsent;

		public static bool IsShowing;

		private string _json;

		public static bool IsInitialized;

		private static bool _isSettingsShowing;

		private void Awake()
		{
			IsInitialized = false;
			if (_instance != null)
			{
				UnityEngine.Object.Destroy(base.gameObject);
				return;
			}
			_instance = this;
			UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		}

		private void Start()
		{
			VoodooSettings voodooSettings = Resources.Load<VoodooSettings>("VoodooSettings");
			if (voodooSettings != null)
			{
				_mainColor = voodooSettings.GdprPrimaryColor;
			}
			VoodooGDPR.Init(voodooSettings);
		}

		public static void Initialize()
		{
			_instance.Init();
			IsInitialized = true;
		}

		private void Init()
		{
			GameObject[] screens = _screens;
			foreach (GameObject gameObject in screens)
			{
				gameObject.gameObject.SetActive(value: true);
			}
			_messageDialog.gameObject.SetActive(value: true);
			_currentScreen = -1;
			GeneratePrivacyPolicies();
			UpdateTextsAndColors(_json);
			GameObject[] screens2 = _screens;
			foreach (GameObject gameObject2 in screens2)
			{
				gameObject2.gameObject.SetActive(value: false);
			}
			_messageDialog.gameObject.SetActive(value: false);
		}

		public static void UpdateConsent(bool adsConsent, bool analyticsConsent)
		{
			_instance.SetAdsConsent(adsConsent);
			_instance.SetAnalyticsConsent(analyticsConsent);
		}

		public static void UpdateTexts(string texts)
		{
			_instance._json = texts;
		}

		public static void ShowMessageDialog(string title, string message, Action closeCallback, string close = "Close")
		{
			_instance._messageDialog.Show(title, message, close, closeCallback);
		}

		private void GeneratePrivacyPolicies()
		{
			RectTransform[] privacyPoliciesContent = _privacyPoliciesContent;
			foreach (RectTransform parent in privacyPoliciesContent)
			{
				string[] allPrivacyPolicies = VoodooGDPRUtils.GetAllPrivacyPolicies();
				foreach (string text in allPrivacyPolicies)
				{
					GameObject gameObject = UnityEngine.Object.Instantiate(_privacyPolicyPrefab);
					gameObject.transform.SetParent(parent, worldPositionStays: false);
					Text component = gameObject.GetComponent<Text>();
					component.text = text;
					Button component2 = gameObject.GetComponent<Button>();
					string url = text;
					component2.onClick.AddListener(delegate
					{
						OpenUrl(url);
					});
				}
			}
		}

		private void UpdateTextsAndColors(string json)
		{
			_jsonData = (json.FromJson<object>() as Dictionary<string, object>);
			if (_jsonData == null)
			{
				VoodooLog.LogE("VoodooGDPRCanvas", json);
				throw new Exception("Couldn't parse text to json.");
			}
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("@COLOR", "#" + ColorUtility.ToHtmlStringRGB(_mainColor));
			dictionary.Add("@GAME_NAME", Application.productName);
			dictionary.Add("@ANALYTICS_SDK_LIST", Environment.NewLine + string.Join(", ", VoodooGDPRUtils.GetSdksNames()));
			dictionary.Add("@ADS_SDK_LIST", Environment.NewLine + string.Join(", ", VoodooGDPRUtils.GetNetworksNames()));
			dictionary.Add("@STUDIO_NAME", "Voodoo");
			dictionary.Add("<br>", Environment.NewLine);
			IdentifiableText[] componentsInChildren = GetComponentsInChildren<IdentifiableText>();
			foreach (IdentifiableText identifiableText in componentsInChildren)
			{
				if (_jsonData.ContainsKey(identifiableText.Identifier))
				{
					identifiableText.SetText(ReplaceAll(_jsonData[identifiableText.Identifier] as string, dictionary));
				}
			}
			ColorElement[] componentsInChildren2 = base.gameObject.GetComponentsInChildren<ColorElement>();
			foreach (ColorElement colorElement in componentsInChildren2)
			{
				colorElement.SetColor(_mainColor);
			}
		}

		public string ReplaceAll(string text, Dictionary<string, string> all)
		{
			string text2 = text;
			foreach (KeyValuePair<string, string> item in all)
			{
				text2 = text2.Replace(item.Key, item.Value);
			}
			return text2;
		}

		public void SetAdsConsent(bool consent)
		{
			_adsConsent = consent;
		}

		public void SetAnalyticsConsent(bool consent)
		{
			_analyticsConsent = consent;
		}

		public void OpenUrl(string url)
		{
			Application.OpenURL(url);
		}

		public void OpenPrivacyPolicy()
		{
			OpenUrl(_jsonData["privacy_policy_url"] as string);
		}

		public void OpenScreen(int index)
		{
			if (index != 5)
			{
				IsShowing = true;
			}
			if (index > _screens.Length || index < 0)
			{
				throw new ArgumentOutOfRangeException();
			}
			if (_currentScreen >= 0)
			{
				_screens[_currentScreen].gameObject.SetActive(value: false);
			}
			_currentScreen = index;
			_screens[_currentScreen].gameObject.SetActive(value: true);
		}

		public void CloseAndGiveConsent(bool overrideConsent)
		{
			if (overrideConsent)
			{
				_adsConsent = true;
				_analyticsConsent = true;
			}
			if (VoodooGDPR.HasLimitAdTrackingEnabled())
			{
				_adsConsent = false;
				_analyticsConsent = false;
			}
			CloseCurrentScreen();
			VoodooGDPR.GiveConsent(_adsConsent, _analyticsConsent);
			if (!_adsConsent || !_analyticsConsent)
			{
				ShowBanner();
			}
		}

		public static void Close()
		{
			_instance.CloseCurrentScreen();
		}

		public void CloseCurrentScreen()
		{
			_screens[_currentScreen].SetActive(value: false);
			IsShowing = false;
			if (_isSettingsShowing)
			{
				VoodooGDPR.CloseSettings();
				_isSettingsShowing = false;
			}
		}

		public void CloseCurrentScreenOrShowWarning()
		{
			if (!_adsConsent || !_analyticsConsent)
			{
				OpenScreen(3);
			}
			else
			{
				CloseAndGiveConsent(overrideConsent: false);
			}
		}

		public static void ShowOptIn()
		{
			_instance.OpenScreen(0);
		}

		public void OpenSettingsMenu()
		{
			_instance.OpenScreen(6);
			_isSettingsShowing = true;
		}

		public void CloseSettingsMenu()
		{
			CloseCurrentScreen();
			VoodooGDPR.CloseSettings();
			_isSettingsShowing = false;
		}

		public static void ShowSettings()
		{
			_instance.OpenSettingsMenu();
		}

		public static void CloseSettings()
		{
			_instance.CloseSettingsMenu();
		}

		public static void ShowBanner()
		{
			if (!IsShowing && IsInitialized)
			{
				_instance.ShowBannerWithAnimation();
			}
		}

		public void CloseBanner()
		{
			StartCoroutine(BannerAnimation(0.3f, reverse: true));
		}

		private void ShowBannerWithAnimation()
		{
			StartCoroutine(BannerAnimation(0.3f, reverse: false));
		}

		private IEnumerator BannerAnimation(float duration, bool reverse)
		{
			if (!reverse)
			{
				OpenScreen(5);
			}
			RectTransform rect = _screens[5].GetComponent<RectTransform>();
			Vector2 pos = rect.anchoredPosition;
			pos.y = ((!reverse) ? 300 : 0);
			float timer = 0f;
			while (timer <= duration)
			{
				timer += Time.unscaledDeltaTime;
				if (reverse)
				{
					pos.y = 300f * Mathf.Clamp01(timer / duration);
				}
				else
				{
					pos.y = 300f * (1f - Mathf.Clamp01(timer / duration));
				}
				rect.anchoredPosition = pos;
				yield return null;
			}
			if (reverse && _currentScreen >= 0)
			{
				CloseCurrentScreen();
			}
		}

		public static bool GetAdsConsent()
		{
			return _instance._adsConsent;
		}

		public static bool GetAnalyticsConsent()
		{
			return _instance._analyticsConsent;
		}
	}
}
